package com.sicnu.cheer.leaugebar.eventmodule.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.adapter.base.CommonAdapter;
import com.sicnu.cheer.generalmodule.adapter.viewholder.CommonViewHolder;
import com.sicnu.cheer.generalmodule.bean.EventBean;
import com.sicnu.cheer.generalmodule.bean.JoinLeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.util.NetWorkUtil;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.contacts.activity.UserInfoActivity;
import com.sicnu.cheer.leaugebar.eventmodule.R;
import com.sicnu.cheer.xlistview.view.XListView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.FindListener;

public class AttendPeopleListActivity extends BaseActivity {

    private LinearLayout noResultLL;
    private XListView listView;
    private CommonAdapter mAdapter;
    private List<MyUser> userList;
    private ProgressDialog progressDialog;
    private int pageSize = 10;
    private int skip = 0;
    private EventBean event;

    @Override
    public int getContentViewId() {
        return R.layout.activity_attend_pelple_list;
    }

    @Override
    protected void initData() {
        super.initData();
        event = (EventBean) getIntent().getSerializableExtra("event");
        userList=new ArrayList<>();
        progressDialog=new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
    }

    @Override
    protected void initView() {
        super.initView();

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ((TextView) findViewById(R.id.title)).setText("参与人员");

        noResultLL = ((LinearLayout) findViewById(R.id.no_data_ll));
        noResultLL.setVisibility(View.GONE);
        listView = ((XListView) findViewById(R.id.data_xlv));
    }
    @Override
    protected void afterViews() {
        super.afterViews();
        setXListView();
        //请求服务器上的数据
        requestDataFromBmob(true);

        //将获取到的数据填充界面
        listView.setAdapter(mAdapter = new CommonAdapter<MyUser>(
                getApplicationContext(), userList, R.layout.item_attend_user) {
            @Override
            public void convert(CommonViewHolder helper, MyUser item) {
                if (item != null) {
                    helper.setImageByUrl(R.id.user_head_iv, item.getHeadPic());
                    helper.setText(R.id.user_name_tv, item.getNick());
                    helper.setText(R.id.user_brief_info_tv, String.valueOf(item.getGrade() + "-") + item.getMajor());
                }
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mThis, UserInfoActivity.class);
                intent.putExtra("user",userList.get(position-1));
                startActivityForResult(intent,1);
            }
        });
    }
    private void setXListView() {
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                pageSize = 10;
                skip = 0;
                requestDataFromBmob(true);
            }

            @Override
            public void onLoadMore() {
                pageSize += 10;
                skip += 10;
                requestDataFromBmob(false);
            }
        });
    }

    private void requestDataFromBmob(final boolean isRefresh) {
        resetState();
        if (!NetWorkUtil.isConnect(mThis)) {
            UIHelper.ToastMessage(mThis, "没有网络，请设置网络");
            return;
        }
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
        BmobQuery<MyUser> query = new BmobQuery<>();
        query.setLimit(pageSize);
        query.setSkip(skip);
        query.addWhereRelatedTo("likes", new BmobPointer(event));
        query.findObjects(mThis, new FindListener<MyUser>() {
            @Override
            public void onSuccess(List<MyUser> list) {
                stopXListView(isRefresh);
                if (list==null||list.isEmpty()){
                    setNoResultState(isRefresh);
                }else {
                    if (isRefresh){
                        userList.clear();
                    }
                    userList.addAll(list);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int i, String s) {
                stopXListView(isRefresh);
                UIHelper.ToastMessage(mThis, "查询失败：" + s);
            }
        });

    }

    private void resetState() {
        noResultLL.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
        listView.setPullLoadEnable(true);
    }

    private void setNoResultState(boolean isRefresh) {
        if (isRefresh){
            noResultLL.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }else {
            listView.setPullLoadEnable(false);
            UIHelper.ToastMessage(mThis, "没有更多数据了");
        }
    }

    private void stopXListView(boolean isRefresh) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        if (isRefresh) {
            listView.stopRefresh();
        } else {
            listView.stopLoadMore();
        }
    }

}
